﻿
@{
    ViewBag.Title = "设置菜单权限";
    Layout = "_Index";
}

<link href="~/plugins/zTree_v3/css/bootstrapStyle/bootstrapStyle.css" rel="stylesheet" />
<div class="ibox">
    <div class="ibox-title">
        当前角色:@(ViewBag.RoleName)
    </div>
    <div class="ibox-content">
        <input type="hidden" id="roleId" value="@(ViewBag.Id)" />
        <ul id="menuTree" class="ztree"></ul>
        <div class="footerbar pull-right">
            <button class="btn btn-primary" type="button" onclick="save()">保存设置</button>
            <button class="btn btn-white" type="button" onclick="cancel()">取消</button>
        </div>
    </div>
</div>
@section scripts
    {
<script src="~/plugins/zTree_v3/js/jquery.ztree.all.min.js"></script>
    <script>
        var index = parent.layer.getFrameIndex(window.name);
        var ptree = null;
        $(function () {
            loadMenuTree();

        });
        function loadMenuTree() {
            var opts = {
                data: {
                    simpleData: {
                        enable: true
                    },
                    key: {
                        checked: 'checkstate'
                    }

                }, callback: {
                    onClick: ZtreeClick
                },
                check: {
                    enable: true,
                    chkStyle: "checkbox",
                    autoCheckTrigger: true,
                    chkboxType: { "Y": "ps", "N": "s" }
                }
            };

            var roleId = $("#roleId").val();
            jutils.ajaxGet('/admin/SysRole/GetRoleMenuTree', { roleId: roleId }, function (res) {
                ptree = $.fn.zTree.init($("#menuTree"), opts, res.data.roleMenu);
                ptree.expandAll(true);

            });
        }

        //单击事件
        function ZtreeClick(event, treeId, treeNode) {
            var productId = treeNode.id;
            //$('#j_datagrid').datagrid('filter', { pid: productId });

        }


        function save() {
            var ids = [];
            var nodes = ptree.getCheckedNodes(true);
            if (nodes.length > 0) {
                //jutils.warn('选择一条数据行编辑');
                //return;
                for (var i = 0; i < nodes.length; i++) {

                    var newobj = {
                        id: nodes[i].id,
                        pId: nodes[i].pId
                    }
                    ids.push(newobj);

                }
            }
            var roleId = $("#roleId").val();
            var rolemenus = JSON.stringify(ids);
            //console.log(rolemenus);
            jutils.confirm('确认修改权限吗', function () {
                jutils.ajaxPost('/admin/sysrole/SaveRoleAuth', { roleId: roleId, ids: rolemenus }, function () {
                    jutils.success('设置完成');
                    parent.layer.close(index);
                });
            });



        }
        function cancel() {
            parent.layer.close(index);
        }
    </script>

}